// 查询服务分类
const axios = require('axios')
const query = {
    row: (id) => `db.collection('type').where({_id:"${id}"}).get()`,
    all: `db.collection("type").orderBy('id', 'order', 'asc').limit(999).get()`,
    get: (params) => `db.collection("type").where({
        name: /${params.name || '.'}/,
    }).orderBy('order', 'asc').limit(${params.pageSize || 10}).skip(${(params.pageSize * (params.pageNo - 1)) || 0}).get()`,
    add: (row) => `db.collection("type").add({
        data: ${JSON.stringify(row)}
      })`,
    del: (id) => "db.collection(\"type\").where({_id:\"" + id + "\"}).remove()",
    update: (id, row) => `db.collection("type").where({_id:"${id}"}).update({
        data: ${JSON.stringify(row)}
    })`
}
const getInfo = (access_token, id) => {
    return axios({
        method: 'POST',
        url: `/tcb/databasequery?access_token=${access_token}`,
        data: {
            env: 'watab-0g38tjg3f9db387b',
            query: query.row(id)
        }
    })
}
const getAll = (access_token) => {
    return axios({
        method: 'POST',
        url: `/tcb/databasequery?access_token=${access_token}`,
        data: {
            env: 'watab-0g38tjg3f9db387b',
            query: query.all
        }
    })
}
const get = (access_token, params) => {
    return axios({
        method: 'POST',
        url: `/tcb/databasequery?access_token=${access_token}`,
        data: {
            env: 'watab-0g38tjg3f9db387b',
            query: query.get(params)
        }
    })
}
const del = (access_token, id) => {
    return axios({
        method: 'POST',
        url: `/tcb/databasedelete?access_token=${access_token}`,
        data: {
            env: 'watab-0g38tjg3f9db387b',
            query: query.del(id)
        }
    })
}
const add = (access_token, row) => {
    return axios({
        method: 'POST',
        url: `/tcb/databaseadd?access_token=${access_token}`,
        data: {
            env: 'watab-0g38tjg3f9db387b',
            query: query.add(row)
        }
    })
}
const update = (access_token, row) => {
    const new_row = {
        ...row
    };
    const { _id } = new_row
    delete row._id
    console.log(query.update(_id, new_row))
    return axios({
        method: 'POST',
        url: `/tcb/databaseupdate?access_token=${access_token}`,
        data: {
            env: 'watab-0g38tjg3f9db387b',
            query: query.update(_id, new_row)
        }
    })
}

module.exports = {
    getInfo,
    getAll,
    get,
    add,
    del,
    update
}